#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint8_t Serial_RxData;
uint8_t Serial_RxFlag;

void Serial_Init(void)
{
	//1.开启RCC时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//GPIO初始化，TX复用输出，RX输入
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//tx
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz速度
	GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//rx
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz速度
	GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化

	//3.配置USART,直接使用一个结构体
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;//波特率，内部会自动计算DIV
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不使用流控
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity =USART_Parity_No;//无奇偶校验
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位
	USART_Init(USART1,&USART_InitStructure);
	
	//4.需要接收的功能，需要配置中断:在开启USART之前，再加上TConfig和NVIC的代码就行了
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//分组
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1,ENABLE);
	
}

/*发送数据*/
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//USART_FLAG_TXE发送数据寄存器空标志位
}

/*发送数组(传递数组需要使用指针)*/
void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
	uint16_t i;
	for(i = 0;i < Length; i++)
	{
		Serial_SendByte(Array[i]);
	}
}

/*发送字符串(字符串自带结束标志位，所以不需要传入长度参数)*/
void Serial_SendString(char *String)
{
	uint8_t i;
	for(i = 0;String[i] != '\0'; i++)
	{
		Serial_SendByte(String[i]);
	}
}

/*指数Result = x^y*/
uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
	uint32_t Result = 1;
	while(Y --)
	{
		Result *= X;
	}
	return Result;
}
	
/*发送一个数字*/
void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for(i = 0;i < Length; i++)
	{
		Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10 + '0');//以10进制从高位向低位发送
	}
	
}

/*重定向printf*/
int fputc(int ch,FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}

/*封装sprintf,格式化字符串*/
void Serial_Printf(char *format,...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);
}


/*查询接收数据在OLED上显示*/
uint8_t Serial_ReceiveCheck(void)
{
	uint8_t RxData;
	if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == SET)
		{
			RxData = USART_ReceiveData(USART1);
		}
		return RxData;
}

/*标志位清除*/
uint8_t Serial_GetRxFlag(void)
{
	if(Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}
/*中断接收*/
uint8_t Serial_GetRxData(void)
{
	return Serial_RxData;
}

/*中断读取*/
void USART1_IRQHandler(void)
{
	if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == SET)
		{
			Serial_RxData = USART_ReceiveData(USART1);
			Serial_RxFlag = 1;
			USART_ClearITPendingBit(USART1,USART_FLAG_RXNE);
		}
}

